<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关联ID" prop="packageableId">
        <el-input
          v-model="queryParams.packageableId"
          placeholder="请输入关联ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购开始时间" prop="purchaseStartTime">
        <el-date-picker clearable
          v-model="queryParams.purchaseStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择限购开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="限购结束时间" prop="purchaseEndTime">
        <el-date-picker clearable
          v-model="queryParams.purchaseEndTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择限购结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="套餐名称" prop="comboName">
        <el-input
          v-model="queryParams.comboName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购人数" prop="persionLimit">
        <el-input
          v-model="queryParams.persionLimit"
          placeholder="请输入限购人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购价格" prop="priceLimit">
        <el-input
          v-model="queryParams.priceLimit"
          placeholder="请输入限购价格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用，0不可用、1可用" prop="availableFlag">
        <el-input
          v-model="queryParams.availableFlag"
          placeholder="请输入是否可用，0不可用、1可用"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:combo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="comboList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="关联ID" prop="packageableId" />
      <el-table-column label="限购开始时间" align="center" prop="purchaseStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.purchaseStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="限购结束时间" align="center" prop="purchaseEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.purchaseEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="套餐名称" align="center" prop="comboName" />
      <el-table-column label="限购人数" align="center" prop="persionLimit" />
      <el-table-column label="限购价格" align="center" prop="priceLimit" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="是否可用，0不可用、1可用" align="center" prop="availableFlag" />
      <el-table-column label="套餐类型，1活动套餐" align="center" prop="type" />
      <el-table-column label="关联类型" align="center" prop="packageableType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:combo:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:combo:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:combo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改活动套餐对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="comboRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联ID" prop="packageableId">
          <el-tree-select
            v-model="form.packageableId"
            :data="comboOptions"
            :props="{ value: 'id', label: 'comboName', children: 'children' }"
            value-key="id"
            placeholder="请选择关联ID"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="限购开始时间" prop="purchaseStartTime">
          <el-date-picker clearable
            v-model="form.purchaseStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择限购开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限购结束时间" prop="purchaseEndTime">
          <el-date-picker clearable
            v-model="form.purchaseEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择限购结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="套餐名称" prop="comboName">
          <el-input v-model="form.comboName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="限购人数" prop="persionLimit">
          <el-input v-model="form.persionLimit" placeholder="请输入限购人数" />
        </el-form-item>
        <el-form-item label="限购价格" prop="priceLimit">
          <el-input v-model="form.priceLimit" placeholder="请输入限购价格" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="是否可用，0不可用、1可用" prop="availableFlag">
          <el-input v-model="form.availableFlag" placeholder="请输入是否可用，0不可用、1可用" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Combo">
import { listCombo, getCombo, delCombo, addCombo, updateCombo } from "@/api/system/combo";

const { proxy } = getCurrentInstance();

const comboList = ref([]);
const comboOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    packageableId: null,
    purchaseStartTime: null,
    purchaseEndTime: null,
    comboName: null,
    persionLimit: null,
    priceLimit: null,
    creator: null,
    updater: null,
    availableFlag: null,
    type: null,
    packageableType: null
  },
  rules: {
    packageableId: [
      { required: true, message: "关联ID不能为空", trigger: "blur" }
    ],
    purchaseStartTime: [
      { required: true, message: "限购开始时间不能为空", trigger: "blur" }
    ],
    purchaseEndTime: [
      { required: true, message: "限购结束时间不能为空", trigger: "blur" }
    ],
    comboName: [
      { required: true, message: "套餐名称不能为空", trigger: "blur" }
    ],
    persionLimit: [
      { required: true, message: "限购人数不能为空", trigger: "blur" }
    ],
    priceLimit: [
      { required: true, message: "限购价格不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    creator: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    updater: [
      { required: true, message: "更新人不能为空", trigger: "blur" }
    ],
    availableFlag: [
      { required: true, message: "是否可用，0不可用、1可用不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "套餐类型，1活动套餐不能为空", trigger: "change" }
    ],
    packageableType: [
      { required: true, message: "关联类型不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询活动套餐列表 */
function getList() {
  loading.value = true;
  listCombo(queryParams.value).then(response => {
    comboList.value = proxy.handleTree(response.data, "id", "packageableId");
    loading.value = false;
  });
}

/** 查询活动套餐下拉树结构 */
function getTreeselect() {
  listCombo().then(response => {
    comboOptions.value = [];
    const data = { id: 0, comboName: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "packageableId");
    comboOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    packageableId: null,
    purchaseStartTime: null,
    purchaseEndTime: null,
    comboName: null,
    persionLimit: null,
    priceLimit: null,
    createTime: null,
    creator: null,
    updateTime: null,
    updater: null,
    availableFlag: null,
    type: null,
    packageableType: null
  };
  proxy.resetForm("comboRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.packageableId = row.id;
  } else {
    form.value.packageableId = 0;
  }
  open.value = true;
  title.value = "添加活动套餐";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.packageableId = row.id;
  }
  getCombo(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改活动套餐";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["comboRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCombo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCombo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除该数据项？').then(function() {
    return delCombo(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
